package cn.edu.zafu.bigdata.x.zbw;

import java.io.*;
import java.net.*;

public class ClockClient {
    public static void main(String[] args) {
        if (args.length < 1) {
            System.out.println("Usage: java ClockClient <host>");
            return;
        }

        try (DatagramSocket socket = new DatagramSocket()) {
            InetAddress serverAddress = InetAddress.getByName(args[0]);
            String sentence = "请求当前时间";
            byte[] sendData = sentence.getBytes();
            byte[] receiveData = new byte[1024];

            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, serverAddress, 9999);
            socket.send(sendPacket);

            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            socket.receive(receivePacket);

            String modifiedSentence = new String(receivePacket.getData(), 0, receivePacket.getLength());
            System.out.println("服务器响应: " + modifiedSentence);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}